package com.sx.micowell.module.homePage.home

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.BaseActivity
import com.example.mvvm.base.data.DataBindingConfig
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
import com.miloyu.modelnet.response.CaloriesRowsVO
import com.miloyu.modelnet.response.DurationRowsVO
import com.miloyu.modelnet.response.PowerKgRowsVO
import com.miloyu.modelnet.response.log
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragHomeAllViewBinding
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.Home2Model
import com.sx.micowell.framework.view.chartManager.*
import com.sx.widget.app.DisplayUtil
import kotlinx.android.synthetic.main.frag_home.*
import kotlin.random.Random

/**
 * @Author: YY
 * @Time: 2021/9/7
 * @Describe 首页 - 全部
 */
@Route(path = RoutePath.HOME_ALL)
class HomeAllFragment : MyFragment<FragHomeAllViewBinding, BaseActivity<*>>() {

    private lateinit var viewModel: Home2Model

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getFragmentScopeViewModel(Home2Model::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_home_all_view, BR.viewModel, viewModel)
    }

    override fun initView() {
        initCourse()
        binding.powerLine.initUi()
        binding.caloriesLine.initHorizontalUIChart()
        binding.monthChart.initUIChart()
    }

    override fun initData() {
        viewModel.reqHome(1)
    }

    @SuppressLint("SetTextI18n")
    override fun initViewObservable() {
        super.initViewObservable()
        // 刷新
        viewModel.ui.refreshEvent.observe(this, {
            binding.refreshLayout.finishRefresh(it)
        })

        viewModel.homeDataEvent.observe(this, {
            // 总时长
            binding.layoutMonth.tvMotionTime.text = "${it.duration}分钟"
            // 总消耗
            binding.layoutMonth.tvCalorie.text = "${it.calorie}"
            // 累计(天)
            binding.layoutMonth.tvRegisterCount.text = "${it.registerCount}"
            // 连续(天)
            binding.layoutMonth.tvRegister.text = "${it.register}"

            val flagRes =
                if (it.powerKgIncrease >= 0) R.drawable.ic_svg_sort else R.drawable.ic_svg_drop
            binding.powerFlag.setImageResource(flagRes)

            // 力量百分比
            binding.powerMin.text = "${it.powerKgIncrease}%"
            // 力量最高
            binding.powerMax.text = "${it.powerKgMax}"

            val durationRes =
                if (it.durationIncrease >= 0) R.drawable.ic_svg_sort else R.drawable.ic_svg_drop
            binding.durationFlag.setImageResource(durationRes)
            // 运动时长百分比
            binding.durationIncrease.text = "${it.durationIncrease}%"
            // 运动时长最大值
            binding.durationMax.text = "${it.durationMax}"

            if (it.powerKgRows.size > 0) {
                powerLineData(it.powerKgRows)
                monthChart(it.powerKgRows)
            }

            if (it.caloriesRows.isNotEmpty()) {
                calorieChartData(it.caloriesRows)
            }

        })
    }

    /**
     * 力量增长趋势
     */
    private fun powerLineData(powerList: List<PowerKgRowsVO>) {
        //data
        binding.powerLine.initData(
            GradientDrawable().also {
                it.shape = GradientDrawable.RECTANGLE
                it.colors = intArrayOf(
                    Color.parseColor("#D2F8EA"),
                    Color.parseColor("#D2F8EA"),
                    Color.parseColor("#E5FBF3"),
                    Color.parseColor(
                        "#FEFFFE"
                    ),
                    Color.WHITE
                )
            },
            powerList.map { Entry(powerList.indexOf(it).toFloat(), it.powerKg.toFloat()) }.toList(),
            powerList.map { it.date }.toList().toTypedArray()
        )
    }

    /**
     * 卡路里分布图
     */
    private fun calorieChartData(caloriesRowsList: List<CaloriesRowsVO>) {
        binding.caloriesLine.initDataChart(
            caloriesRowsList.map {
                BarEntry(
                    caloriesRowsList.indexOf(it).toFloat(),
                    it.calorie.toFloat()
                )
            }.toList(), caloriesRowsList.map { it.title }.toList().toTypedArray()
        )
    }

    /**
     * 月运动趋势分布图
     */
    private fun monthChart(powerList: List<PowerKgRowsVO>) {
        for (powerKgRowsVO in powerList) {
            powerKgRowsVO.toString().log()
        }
        binding.monthChart.initDataChart(
            powerList.map { BarEntry(powerList.indexOf(it).toFloat(), it.duration.toFloat()) }
                .toList(), powerList.map { it.date }.toList().toTypedArray()
        )
    }

    /**
     * 推荐课程
     */
    private fun initCourse() {

        childFragmentManager.beginTransaction()
            .add(R.id.home_frag, RouteManager.getInstance().getRecommendCourseFragment().also {
//                it.setOnHeightListener {
//                    "initCourse${it}".log()
//                    home_frag.post {
//                        var layoutParams = home_frag.layoutParams
//                        layoutParams.height = it
//                        home_frag.layoutParams=layoutParams
//                    }
//                }
            }).commit()
    }
}